A new EPL library has been added to support programming in a functional style for quicker development, fewer for
loops, and more concise and readable code. For example:
// Calculate the average of the values in the sequence = 20
Fn.reduce([10, 20, 30], Fn.mean());
// Filter strings by regular expression using the string.matches action = ["Hello Alice", "Hello Al"]
Fn.filter(["Hello Alice", "Hello Bob", "Hello Al"], Fn.callAction("matches", ["Hello Al.*"]));
// Use Functional() to chain multiple operations: filter out odd numbers, then reduce the sequence to a single value containing the sum = 70
Functional([10, -5, 20, 40]).filter(Fn.even).reduce(Fn.sum);
The library provides similar capabilities to Python’s functools
module. It can be added to Apama projects with a new Functional EPL Library bundle which provides two main EPL types: com.apama.functional.Fn
and com.apama.functional.Functional
. These provide functional operations such as map
, filter
and reduce
in both a functional style and a fluent style. They operate on EPL sequence
and dictionary
containers and on a new “generator” concept that lazily calculates infinite lists.
There is support for partial binding of action arguments:
Fn.map(["Bob", "Alice"], Fn.partial(Fn.concat, "Hello ")).toString(); // = ["Hello Bob","Hello Alice"]
There are also some new actions to make event sending and listening more concise, including the following:
// Initialize an event (providing only the subset of fields you care about), send and extract the request id for use in listener
// (it is also possible to initialize event fields by name rather than by position)
on MyResponse(reqId=<integer> Fn.sendToChannel(MyEvent.SEND_CHANNEL,
Fn.setFields(new MyEvent, [<any>createRequestId(), "/myrequest"])
).getEntry("reqId") ) as response
{
...
}
// Listen for events with an identifier matching each value from this sequence, and calls a completion action when all have arrived
// (similar to an EPL "and" event expression). A different callback is executed if the specified timeout expires.
Functional(sequenceIDs)
.waitForAllCompleted("MyResponse", "id", onCompleted)
.onTimeout(TIMEOUTSECS, onTimeout);
For detailed information, see Using functional operations in EPL. See also the API reference for EPL (ApamaDoc) for detailed information on com.apama.functional
.